package com.chenglu.android.classify;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Outline;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chenglu.android.R;
import com.chenglu.android.classify.adapter.ClassifyTwoAdapter;
import com.chenglu.android.classify.bean.CategoryBannerBean;
import com.chenglu.android.classify.bean.ClassisyTreeBean;
import com.chenglu.android.classify.mvp.contract.ClassifyTreeContract;
import com.chenglu.android.classify.mvp.presenter.ClassifyTreePresenter;
import com.chenglu.android.common.base.BaseMvpFragment;
import com.chenglu.android.common.utils.GlideUtils;
import com.chenglu.android.common.utils.LogUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.core.CenterPopupView;

import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.TabAdapter;
import q.rorbin.verticaltablayout.widget.ITabView;
import q.rorbin.verticaltablayout.widget.TabView;


/**
 * @author: lyj
 * @Created: by Copyright 2023/3/6 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：分类
 */
public class ClassifyTreeFragment extends BaseMvpFragment<ClassifyTreePresenter> implements ClassifyTreeContract.View {

    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tablayout1)
    VerticalTabLayout tablayout1;

    private ClassifyTwoAdapter classifyTwoAdapter;


    public static ClassifyTreeFragment newInstance() {
        ClassifyTreeFragment classifyTreeFragment = new ClassifyTreeFragment();
        return classifyTreeFragment;
    }

    @Override
    protected void initView() {
        mPresenter = new ClassifyTreePresenter();
        mPresenter.attachView(this);

    }



    private void initAdapter() {
        List<ClassisyTreeBean.Data.ChildrenX> twoList = new ArrayList<>();
        classifyTwoAdapter = new ClassifyTwoAdapter(mContext, twoList);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(classifyTwoAdapter);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_classify_tree;
    }


     boolean isfirstShow = true;
    @Override
    public void onResume() {
        super.onResume();

        ImmersionBar.with(this)
                .statusBarColor(R.color.white)
                .navigationBarColor(R.color.white)
                .fitsSystemWindows(true)
                .autoDarkModeEnable(true)
                .statusBarDarkFont(true)
                .init();

        if(isfirstShow){
            mPresenter.getClassisyTree();
            isfirstShow= false;
        }

    }

    @Override
    public void onError(String errorMsg) {
//        isLogin = false;
//        SPUtils.setBoolean("ISLOGIN", false);
//        showEmptyView("暂无数据");
//        showXPopupView(context, "提示", "登录已过期,请重新登录", "登录");
    }


    @Override
    public void getClassisyTree(ClassisyTreeBean bean) {


        List<ClassisyTreeBean.Data.ChildrenX> twoList = new ArrayList<>();
        classifyTwoAdapter = new ClassifyTwoAdapter(mContext, twoList);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(classifyTwoAdapter);

        tablayout1.setTabAdapter(new TabAdapter() {
            @Override
            public int getCount() {//个数
                return bean.getData().size();
            }

            @Override
            public ITabView.TabBadge getBadge(int position) {

                return null;
            }

            @Override
            public ITabView.TabIcon getIcon(int position) {
                return null;
            }

            @Override
            public ITabView.TabTitle getTitle(int position) {//设置标题
                ITabView.TabTitle title = new ITabView.TabTitle.Builder()
                        .setContent(bean.getData().get(position).getTitle())//从集合中获取标题
                        .setTextColor(Color.parseColor("#E53B1A"),Color.BLACK)
                        .setTextSize(14)
                        .build();
                return title;

            }

            @Override//设置颜色
            public int getBackground(int position) {
                return R.color.color_f8f8f8;
            }
        });


        tablayout1.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {

                ClassisyTreeBean.Data data = bean.getData().get(position);

                    twoList.clear();
                    if(data.getHasChildren()){
                        for (int i = 0; i <data.getChildren().size() ; i++) {
                            twoList.add(data.getChildren().get(i));
                        }
                    }

                    classifyTwoAdapter.notifyDataSetChanged();


            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });


        //添加数据 0的数据

            if( bean.getData().get(0).getHasChildren()){
                for (int j = 0; j < bean.getData().get(0).getChildren().size(); j++) {

                    twoList.add(bean.getData().get(0).getChildren().get(j));
                }

                LogUtils.d("集合的数据"+twoList.size());

        }
          classifyTwoAdapter.notifyDataSetChanged();

    }


    @Override
    public void getBanner(CategoryBannerBean bean) {

        List<String> images = new ArrayList();
        initBannerData(bean, banner, images);
    }


    //设置banner数据
    private void initBannerData(CategoryBannerBean item, Banner banner, List<String> images) {
        for (CategoryBannerBean.Data b : item.getData()) {
            images.add(b.getImg());
        }
        //设置图片加载器
        banner.setImageLoader(new MyImageLoader());
        //设置图片集合
        banner.setImages(images);
        //设置轮播的动画效果
        banner.setBannerAnimation(Transformer.Default);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间（设置2.5秒切换下一张图片）
        banner.setDelayTime(2500);
        //设置banner显示样式（带标题的样式）
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //图片设置成圆角
        banner.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 15);
            }
        });

        banner.setClipToOutline(true);
        banner.start();
    }

    //定义图片加载器
    public class MyImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {

            GlideUtils.setImageView(context, path, imageView, 8);

        }
    }


    //*************************************start 对话框********************************************//
   CustomPopup popupView;

    //弹窗登录的对话框
    public void showXPopupView(Context context, String title, String content, String confirmBtnText) {

        popupView = new CustomPopup(context);
        popupView.title =title;
        popupView.content =content;
        popupView.comfrim =confirmBtnText;
        new XPopup.Builder(context)
                .dismissOnTouchOutside(false)
                .asCustom(popupView)
                .show();
    }



    class CustomPopup extends CenterPopupView {
        //注意：自定义弹窗本质是一个自定义View，但是只需重写一个参数的构造，其他的不要重写，所有的自定义弹窗都是这样。
        public CustomPopup(@NonNull Context context) {
            super(context);
        }

        // 返回自定义弹窗的布局
        @Override
        protected int getImplLayoutId() {
            return R.layout.dialog_login_layout;
        }

        // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
        @Override
        protected void onCreate() {
            super.onCreate();
            TextView tvTitle = findViewById(R.id.tv_dialog_title);
            TextView tvContent = findViewById(R.id.tv_dialog_content);
            TextView tvComfirm= findViewById(R.id.tv_dialog_comfirm);

            tvTitle.setText(title);
            tvContent.setText(content);
            tvComfirm.setText(comfrim);
            tvComfirm.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
//                    startActivity( new Intent(context, LoginActivity.class));
//                    getActivity().finish();
                }
            });


        }

        // 设置最大宽度，看需要而定，
        @Override
        protected int getMaxWidth() {
            return super.getMaxWidth();
        }

        // 设置最大高度，看需要而定
        @Override
        protected int getMaxHeight() {
            return super.getMaxHeight();
        }

        // 设置自定义动画器，看需要而定
        @Override
        protected PopupAnimator getPopupAnimator() {
            return super.getPopupAnimator();
        }

        /**
         * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
         *
         * @return
         */
        protected int getPopupWidth() {
            return 0;
        }

        /**
         * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
         *
         * @return
         */
        protected int getPopupHeight() {
            return 0;
        }

        public String title;
        public  String content;
        public String comfrim;

    }


}
